using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using DTO;
using BUS;
using Utility;
public partial class admin_A_NhanVien : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    private void LoadData()
    {

        //try
        //{
        //    string tendangnhap = Session["TaiKhoan"].ToString();
        //    if (tendangnhap != "")
        //    {
        //        lit_tennguoidung.Text = Session["TaiKhoan"].ToString();

        //    }
        //    else
        //    {
        //        Session["taikhoan"] = "";
        //    }
        //}
        //catch (Exception ex)
        //{
        //    Response.Redirect("~/admin/A_dangnhap.aspx");
        //}
        try
        {
            //string tendangnhap = Session["TaiKhoan"].ToString();
            //if (tendangnhap != "")
            //{
            //    lit_tennguoidung.Text = Session["TaiKhoan"].ToString();

            //}
            //else
            //{
            //    Session["taikhoan"] = "";
            //}
            //int id = int.Parse(Session["manv"].ToString());
            //HyperLink1.NavigateUrl = "~/admin/A_suanhanvien.aspx?id=" + id;
        }
        catch (Exception ex)
        {
            Response.Redirect("~/admin/A_dangnhap.aspx");
        }
        Pager.PageSize = 5;
        Criteria c = new Criteria();

        if (txt_TenNV.Text != null)
        {
            c.add(Condition.AND, tbNhanVienColumns.TenNV, Condition.LIKE,
                txt_TenNV.Text);

        }
        int total = 0;
        tbNhanVienCollection nhanvienCollection =
            NHANVIEN_BUS.Get(c.Criter, Pager.CurrentIndex, Pager.PageSize,
            tbNhanVienColumns.MaNV.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = nhanvienCollection;
        Repeater1.DataBind();

        Pager.ItemCount = total;
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
            e.Item.ItemType == ListItemType.Item)
        {
            NhanVien_DTO nhanvien = (NhanVien_DTO)e.Item.DataItem;
            HyperLink hyp_TenNv = (HyperLink)e.Item.FindControl("hyp_TenNv");
            Literal lit_matkhau = (Literal)e.Item.FindControl("lit_matkhau");
            Literal lit_taikhoan = (Literal)e.Item.FindControl("lit_taikhoan");
            Literal lit_maquyen = (Literal)e.Item.FindControl("lit_maquyen");
            Literal lit_diaChi = (Literal)e.Item.FindControl("lit_diaChi");
            Literal lit_dienThoai = (Literal)e.Item.FindControl("lit_dienThoai");
            Literal lit_Mail = (Literal)e.Item.FindControl("lit_Mail");

            hyp_TenNv.Text = nhanvien.TenNV;
            hyp_TenNv.NavigateUrl = "~/admin/A_Nhanvien_update.aspx?id=" + nhanvien.MaNV.ToString();

            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");
            hpl_Edit.NavigateUrl = "~/admin/A_NhanVien_update.aspx?id=" + nhanvien.MaNV.ToString();

        }
    }
    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        Pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        LoadData();
    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < Repeater1.Items.Count; i++)
            {
                CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
                if (cb_Xoa.Checked == true)
                {
                    HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                    int id = int.Parse(hf_id.Value);
                    NhanVien_DTO nhanvien = NHANVIEN_BUS.GetByID(id);
                    NHANVIEN_BUS.delete(id);
                }
            }
        }
        catch(Exception ex) {
            Response.Redirect(Request.RawUrl);
                
        }
        Response.Redirect(Request.RawUrl);
    }
}
